<html>
<head>
<style type="text/css">

table{	
border-collapse:collapse;
table-layout:fixed;	
}
td.desc
{
width:6.5em;
}
tr.tables
{
border:1px solid darkgrey;
background-color:black;
color:white;
}

tr.attributes
{
border:1px solid white;
background-color:#303030;
color:white;
}
tr.rowa
{
border:1px solid white;
background-color:#D0D0D0;
color:black;
}
tr.rowb
{
border:1px solid white;
background-color:white;
color:black;
}
</style>
</head>
<body>

<script language="JavaScript">
<!--
function disable(disableIt)
{
	document.addForm.Dday.disabled = disableIt;
	document.addForm.Dmonth.disabled = disableIt;
	document.addForm.Dyear.disabled = disableIt;

}
//-->
</script>

<?php
$id=$_GET["id"];

print "<div align=center><form action=search.php method=post>";
print "<input type=text maxlength=100 name=query size='150' value=\"". htmlspecialchars($raw_query)."\"></input>";
print "<input type=submit  value=Run></input></form><br>";

//SOuRCE FOR LINKS HERE
print "<div align=center><table width=800>
		<tr class=rowb>
		<td><a href=addmovie.php><b>Add Movie</b></a></td>
		<td><a href=addmovieactor.php><b>Add Movie Actor</b></a></td>
		<td><a href=addmoviedirector.php><b>Add Movie Director</b></a></td>
		<td><a href=add_actor_director.php><b>Add Actor or Director</b></a>	</td>
		</tr>
		</table></div>";

function connect_to_server(){
	$DB="CS143";
	$db_connection = mysql_connect("localhost", "cs143", "");
	if(!$db_connection) {
		$errmsg = mysql_error($db_connection);
		print "Connection failed: " .$errmsg ."<br />";
		exit(1);
	}
	mysql_select_db("$DB", $db_connection);
	return $db_connection;

}

function make_query($command, $db_connection){
		
	$resTable = mysql_query($command, $db_connection);
	if(!$resTable) {
		$errmsg = mysql_error($db_connection);

		if($command != ""){
			print "Query fetch failed: " .$errmsg ."<br />";
		}

		exit(1);
	
	}
	return $resTable;
	
}

$raw_fname=$_POST["fname"];
$raw_lname=$_POST["lname"];

$dayB=$_POST["Bday"];
$monthB=$_POST["Bmonth"];
$yearB=$_POST["Byear"];

$dayD=$_POST["Dday"];
$monthD=$_POST["Dmonth"];
$yearD=$_POST["Dyear"];

$sex=$_POST["sex"];
$identity=$_POST["identity"];

$month["January"] = "1";
$month["February"] = "2";
$month["March"] = "3";
$month["April"] = "4";
$month["May"] = "5";
$month["June"] = "6";
$month["July"] = "7";
$month["August"] = "8";
$month["September"] = "9";
$month["October"] = "10";
$month["November"] = "11";
$month["December"] = "12";

$error = false;

print "<br>";
print "<h3>Add an Actor/Director</h3>";
print "<form name = addForm action=add_actor_director.php method=post>";
print "<table>";
	print "<tr><td class=desc>Identity </td><td><input type=radio name=identity value=Actor "; if(isset($_POST["identity"]) && $_POST["identity"] == "Actor") print "checked=checked"; print "/> Actor "; 
	print "<input type=radio name=identity value=Director "; if(isset($_POST["identity"]) && $_POST["identity"] == "Director") print "checked=checked"; print " /> Director</td></tr>";
	if(!(isset($_POST["identity"])) && isset($_POST[addAD]))
	{
		print "<b><font color=red>Error: Please choose either actor or director.</font></b><br>";
		$error=true;
	}

	print "<tr><td class=desc>First Name </td><td><input type=text NAME=fname SIZE=20 MAXLENGTH=20 value=" . htmlentities($raw_fname, ENT_QUOTES) . "></td></tr>";
	if($_POST["fname"] == null && isset($_POST[addAD])){
		print "<b><font color=red>Error: First name cannot be null.</font></b><br>";
		$error=true;
	}
	print "<tr><td>Last Name </td><td><input type=text NAME=lname SIZE=20 MAXLENGTH=20 value=" . htmlentities($raw_lname, ENT_QUOTES) ." ></td></tr>";
	if($_POST["lname"] == null && isset($_POST[addAD])){
		print "<b><font color=red>Error: Last name cannot be null.</font></b><br>";
		$error=true;
	}

	print "<tr><td class=desc>Sex </td><td><input type=radio name=sex value=Male "; if(isset($_POST["sex"]) && $_POST["sex"] == "Male") print "checked=checked"; print "/> Male "; 
	print "<input type=radio name=sex value=Female "; if(isset($_POST["sex"]) && $_POST["sex"] == "Female") print "checked=checked"; print " /> Female</td></tr>";
	if(!(isset($_POST["sex"])) && isset($_POST[addAD]))
	{
		print "<b><font color=red>Error: Please choose a gender.</font></b>";
		$error=true;
	}

$days = range(1, 31);
$years = range(1900, date("Y"));
$years_current = array_reverse($years);

print "<tr><td class=desc>Date of Birth</td><td> 
<select name=Bday>";
	foreach($days as $day)
	{
		print "<option value=$day ";
		if(isset($_POST["Bday"]) && $_POST["Bday"] == $day) print "selected=selected";
		print ">$day</option>";
	}
print "</select>";

print "<select name=Bmonth>";
	print "<option value=January "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "January") print "selected=selected"; print ">January</option>";
	print "<option value=February "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "February") print "selected=selected"; print ">February</option>";
	print "<option value=March "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "March") print "selected=selected"; print ">March</option>";
	print "<option value=April "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "April") print "selected=selected"; print ">April</option>";
	print "<option value=May "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "May") print "selected=selected"; print ">May</option>";
	print "<option value=June "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "June") print "selected=selected"; print ">June</option>";
	print "<option value=July "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "July") print "selected=selected"; print ">July</option>";
	print "<option value=August "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "August") print "selected=selected"; print ">August</option>";
	print "<option value=September "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "September") print "selected=selected"; print ">September</option>";
	print "<option value=October "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "October") print "selected=selected"; print ">October</option>";
	print "<option value=November "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "November") print "selected=selected"; print ">November</option>";
	print "<option value=December "; if(isset($_POST["Bmonth"]) && $_POST["Bmonth"] == "December") print "selected=selected"; print ">December</option>";
print "</select>";

print "<select name=Byear>";
	foreach($years_current as $year)
	{
		print "<option value=$year ";
		if(isset($_POST["Byear"]) && $_POST["Byear"] == $year) print "selected=selected";
		print ">$year</option>";
	}
print "</select></td></tr>";

print "<tr><td class=desc>Date of Death</d><td><select name=Dday "; if(isset($_POST["alive"]) && $_POST["alive"] == "yes") print "disabled=disabled"; print ">";
	foreach($days as $day)
	{
		print "<option value=$day ";
		if(isset($_POST["Dday"]) && $_POST["Dday"] == $day) print "selected=selected";
		print ">$day</option>";
	}
print "</select>";

print "<select name=Dmonth "; if(isset($_POST["alive"]) && $_POST["alive"] == "yes") print "disabled=disabled"; print ">";
	print "<option value=January "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "January") print "selected=selected"; print ">January</option>";
	print "<option value=February "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "February") print "selected=selected"; print ">February</option>";
	print "<option value=March "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "March") print "selected=selected"; print ">March</option>";
	print "<option value=April "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "April") print "selected=selected"; print ">April</option>";
	print "<option value=May "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "May") print "selected=selected"; print ">May</option>";
	print "<option value=June "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "June") print "selected=selected"; print ">June</option>";
	print "<option value=July "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "July") print "selected=selected"; print ">July</option>";
	print "<option value=August "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "August") print "selected=selected"; print ">August</option>";
	print "<option value=September "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "September") print "selected=selected"; print ">September</option>";
	print "<option value=October "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "October") print "selected=selected"; print ">October</option>";
	print "<option value=November "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "November") print "selected=selected"; print ">November</option>";
	print "<option value=December "; if(isset($_POST["Dmonth"]) && $_POST["Dmonth"] == "December") print "selected=selected"; print ">December</option>";
print "</select>";

print "<select name=Dyear "; if(isset($_POST["alive"]) && $_POST["alive"] == "yes") print "disabled=disabled"; print ">";
	foreach($years_current as $year)
	{
		print "<option value=$year ";
		if(isset($_POST["Dyear"]) && $_POST["Dyear"] == $year) print "selected=selected";
		print ">$year</option>";
	}
print "</select> ";
print "Still Alive?<input type=checkbox name=alive value=yes onclick=disable(this.checked) ";
empty($_POST['alive']) ? print '' : print "checked=checked";
print " /></td></td>";
print "</table><br>";
print "<input type=submit name=addAD  value=Submit></form><br>";

//print "First name: $raw_fname<br>";
//print "Last name: $raw_lname<br>";

//print "Birth day: $dayB<br>";
//print "Birth month: $monthB<br>";
//print "Birth year: $yearB<br>";

//print "Death day: $dayD<br>";
//print "Death month: $monthD<br>";
//print "Death year: $yearD<br>";

//print "Sex: $sex<br>";
//print "Identity: $identity<br>";
//print "alive:" . $_POST["alive"] . "<br>";
//print "Birth Month number:" . $month[$monthB] . "<br>";


//Open a connection to the server for a database.
$db_conn=connect_to_server();

$sanitized_fname = mysql_real_escape_string($raw_fname, $db_conn);
$sanitized_lname = mysql_real_escape_string($raw_lname, $db_conn);

$dobConcat = $yearB."-".$month[$monthB]."-".$dayB;

if($yearD != "" && $month[$monthD] != "" && $dayD != "")
{
	$dodConcat = $yearD."-".$month[$monthD]."-".$dayD;
}
else
{
	$dodConcat = "";
}

//print "dob: " . $dobConcat . "<br>";
//print "dod: " . $dodConcat;

if(isset($_POST["addAD"])) {
	if($error == false)
	{
		if($identity == "Actor"):

			if($dodConcat == ""):
				$Table = "Actor(last,first,sex,dob)";
				$query = "INSERT INTO " . $Table . " VALUES (\"$sanitized_lname\", \"$sanitized_fname\", \"$sex\", \"$dobConcat\")";
			else:
				$Table = "Actor(last,first,sex,dob,dod)";
				$query = "INSERT INTO " . $Table . " VALUES (\"$sanitized_lname\", \"$sanitized_fname\", \"$sex\", \"$dobConcat\", \"$dodConcat\")";
			endif;

		elseif($identity == "Director"):

			if($dodConcat == ""):
				$Table = "Director(last,first,dob)";
				$query = "INSERT INTO " . $Table . " VALUES (\"$sanitized_lname\", \"$sanitized_fname\", \"$dobConcat\")";
			else:
				$Table = "Director(last,first,dob,dod)";
				$query = "INSERT INTO " . $Table . " VALUES (\"$sanitized_lname\", \"$sanitized_fname\", \"$dobConcat\", \"$dodConcat\")";
			endif;

		else:
			$query = "";
		endif;

		make_query($query, $db_conn);

		print "<p style=font-family:arial;font-size:110%;color:#4CC417> Sucessfully added $identity ".htmlentities($raw_fname, ENT_QUOTES)." ".htmlentities($raw_lname, ENT_QUOTES)."! </p>";
	}
}



mysql_close($db_conn);
print "</div>";

?>
<br>
</body>
</html>
